<?php
$caption = BUtils::T('overview_lbl');
$this->pageTitle = $caption;

$this->breadcrumbs = array(
	$caption,
);

//running days
$differ = time() - Yii::app()->user->start_ts;
$days_string = sprintf('%d', round($differ / 3600 / 24));

//stats calculate
$reach = Yii::app()->locale->numberFormatter->formatDecimal($offers['reach']);
$notifications = Yii::app()->locale->numberFormatter->formatDecimal($offers['notifications']);
$people = Yii::app()->locale->numberFormatter->formatDecimal($customers['people']);
$visits = Yii::app()->locale->numberFormatter->formatDecimal($customers['visits']);

//current location
$latitude = Yii::app()->user->getState('latitude');
$longitude = Yii::app()->user->getState('longitude');
if (!empty($latitude) and !empty($longitude)) {
	$location_js_array = "[{$latitude}, {$longitude}]";
} else {
	$location_js_array = 'null';
}

//stats.
$last_7_days = date(BUtils::T('php_date_long'), mktime(0, 0, 0, date('m'), date('d')-7)) . ' - ' . date(BUtils::T('php_date_long'));
$last_28_days = date(BUtils::T('php_date_long'), mktime(0, 0, 0, date('m'), date('d')-28)) . ' - ' . date(BUtils::T('php_date_long'));
$today_date = date(BUtils::T('php_date_long'));
$yesterday_date = date(BUtils::T('php_date_long'), mktime(0, 0, 0, date('m'), date('d')-1));
?>
<div class="page-header position-relative">
	<h1><?php echo $caption; ?></h1>
</div><!--/.page-header-->

<div class="row-fluid">
	<div class="row-fluid">
		<div class="span4">
			<div class="alert alert-block alert-info">
				<button type="button" class="close" data-dismiss="alert">
					<i class="icon-remove"></i>
				</button>
				<h5>Welcome to Yamii for Business!</h5>
				<p></p>
			</div>
		</div>
		<div class="span4 center">
			<div class="infobox infobox-green bigger">
				<div class="infobox-icon pull-left">
					<i class="icon-group"></i>
				</div>
				<div class="infobox-data">
					<span class="infobox-data-number"><?php echo $visits; ?></span>
					<div class="infobox-content"><strong><?php echo BUtils::T('total_visits_lbl'); ?></strong></div>
					<div class="hr hr8"></div>
					<span class="infobox-data-number-sub"><?php echo $people; ?></span>
					<div class="infobox-content-sub"><?php echo BUtils::T('customers_lbl'); ?></div>
				</div>
			</div>
		</div>
		<div class="span4 center">
			<div class="infobox infobox-orange3 bigger">
				<div class="infobox-icon">
					<i class="icon-certificate"></i>
				</div>
				<div class="infobox-data">
					<span class="infobox-data-number"><?php echo $reach; ?></span>
					<div class="infobox-content"><strong><?php echo BUtils::T('total_reach_lbl'); ?></strong></div>
					<div class="infobox-content"></div>
					<div class="hr hr8"></div>
					<span class="infobox-data-number-sub"><?php echo $notifications; ?></span>
					<div class="infobox-content-sub"><?php echo BUtils::T('notifications_lbl'); ?></div>
				</div>
			</div>
		</div>
	</div>
	<div class="space"></div>
	
	<div class="row-fluid">
		<div class="span12">
			<h3 class="row-fluid header lighter">
				<div class="span6">
					<i class="icon-group green"></i> <?php echo BUtils::T('customers_stats_lbl'); ?>
				</div>
				<div class="span6"></div>
			</h3>
			<div class="row-fluid clearfix">
				<div class="span6">
					<div class="widget-box transparent">
						<div class="widget-header widget-header-flat">
							<h4 class="lighter">
								<?php echo BUtils::T('customer_report_lbl'); ?>
							</h4>
							<div class="widget-toolbar">
								<a href="#" id="customer-report-btn" data-action="reload">
									<i class="icon-refresh"></i>
								</a>
							</div>
						</div>
						<div class="" style="padding:8px 0; text-align:left;">
							<div class="btn-group">
								<button class="btn " data-toggle="dropdown" style="border-bottom-right-radius:4px;border-top-right-radius:4px;">
									<i class="icon-calendar"></i>
									<span id="customer-period-text"><?php echo BUtils::T('today_lbl'); ?></span>
									<span class="caret"></span>
								</button>
								<ul class="dropdown-menu dropdown-default" id="customer-period-list">
									<li>
										<a href="#today" data-type="today" data-value="<?php echo $today_date; ?>"><?php echo BUtils::T('today_lbl'); ?></a>
									</li>
									<li>
										<a href="#yesterday" data-type="yesterday" data-value="<?php echo $yesterday_date; ?>"><?php echo BUtils::T('yesterday_lbl'); ?></a>
									</li>
									<li>
										<a href="#last_7" data-type="last_7" data-value="<?php echo $last_7_days; ?>"><?php echo BUtils::T('last_num_days_lbl', array('{number}'=>7)); ?></a>
									</li>
									<li>
										<a href="#last_28" data-type="last_28" data-value="<?php echo $last_28_days; ?>"><?php echo BUtils::T('last_num_days_lbl', array('{number}'=>28)); ?></a>
									</li>
								</ul>
							</div>
							<span id="customer-period-date" style="margin-left:8px;"><?php echo $today_date; ?></span>
							<input id="customer-period-selected" type="hidden" value="today" />
						</div>
						<div class="widget-body">
							<div class="widget-main" id="customer-report">
								<div id="customer-report-error" style="display:none;">
									<?php BHelper::HtmlErrorWell(); ?>
								</div>
							</div>
						</div><!--/widget-body-->
					</div><!--/widget-box-->
				</div>
				<div class="span6">
					<div class="widget-box transparent">
						<div class="widget-header widget-header-flat">
							<h4 class="lighter">
								<?php echo BUtils::T('customers_recent_lbl'); ?>
								<small>
									(<a href="<?php echo Yii::app()->createUrl('/customer'); ?>"><?php echo BUtils::T('customer_list_lbl'); ?></a>)
								</small>
							</h4>
							<div class="widget-toolbar">
								<a href="#" id="customers-recent-btn" data-action="reload">
									<i class="icon-refresh"></i>
								</a>
								<a href="#" data-action="collapse">
									<i class="icon-chevron-up"></i>
								</a>
							</div>
						</div>
						<div class="alert alert-info">
							<?php echo BUtils::T('customers_recent_eg'); ?>
						</div>
						<div class="widget-body">
							<div class="widget-main" id="customers-recent">
								<div id="customers-recent-error" style="display:none;">
									<?php BHelper::HtmlErrorWell(); ?>
								</div>
							</div>
						</div><!--/widget-body-->
					</div><!--/widget-box-->
				</div>
			</div>
		</div>
	</div>
	<div class="space"></div>
	
	<div class="row-fluid">
		<div class="span12">
			<h3 class="header lighter">
				<i class="icon-certificate orange"></i> <?php echo BUtils::T('offers_stats_lbl'); ?>
			</h3>
			<div class="row-fluid clearfix">
				<div class="span6">
					<div class="widget-box transparent">
						<div class="widget-header widget-header-flat">
							<h4 class="lighter"><?php echo BUtils::T('offers_results_lbl'); ?></h4>
							<div class="widget-toolbar">
								<a href="#" id="offers-stats-btn" data-action="reload">
									<i class="icon-refresh"></i>
								</a>
							</div>
						</div>
						<div class="widget-body">
							<div class="widget-main" id="offers-stats">
								<div id="offers-stats-error" style="display:none;">
									<?php BHelper::HtmlErrorWell(); ?>
								</div>
							</div>
						</div><!--/widget-body-->
					</div><!--/widget-box-->
				</div>
				<div class="span6">
					<div class="widget-box transparent">
						<div class="widget-header widget-header-flat">
							<h4 class="lighter">
								<?php echo BUtils::T('today_offers_lbl'); ?>
								<small>
									(<a href="<?php echo Yii::app()->createUrl('/offer'); ?>"><?php echo BUtils::T('offers_list_lbl'); ?></a>)
								</small>
							</h4>
							<div class="widget-toolbar">
								<a href="#" id="offers-recent-btn" data-action="reload">
									<i class="icon-refresh"></i>
								</a>
							</div>
						</div>
						<div class="widget-body">
							<div class="widget-main" id="offers-recent">
								<div id="offers-recent-error" style="display:none;">
									<?php BHelper::HtmlErrorWell(); ?>
								</div>
							</div>
						</div><!--/widget-body-->
					</div><!--/widget-box-->
				</div>
			</div>
		</div>
	</div>
	
	<div class="space"></div>
</div>

<!-- Modal -->
<div id="location-model" class="modal hide" role="dialog" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3><?php BUtils::T('confirm_location_lbl'); ?></h3>
	</div>
	<div class="modal-body">
		<p><img src="<?php echo Yii::app()->baseUrl; ?>/img/ajax-loaders/ajax-loader-7.gif" /></p>
	</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true"><?php BUtils::T('close_btn'); ?></button>
		<button class="btn btn-primary" id="save-location"><?php BUtils::T('save_changes_btn'); ?></button>
	</div>
</div>
<audio id="soundHandle" style="display: none;"></audio>
<script>
$(function() {
	//load customers stats
	$('#customer-report-btn').click(function(ev){
		load_customer_report($('#customer-period-selected').val());
	});
	//load recent customers
	$('#customers-recent-btn').click(function(ev){
		load_recent_customers();
	});
	//load offers stats
	$('#offers-stats-btn').click(function(ev){
		load_offers_stats();
	});
	//load recent offers
	$('#offers-recent-btn').click(function(ev){
		load_recent_offers();
	});
	
	//refresh
	setTimeout(function(){ $('#customer-report-btn').trigger('click'); }, 50);
	setTimeout(function(){ $('#customers-recent-btn').trigger('click'); }, 500);
	setTimeout(function(){ $('#offers-recent-btn').trigger('click'); }, 1000);
	setTimeout(function(){ $('#offers-stats-btn').trigger('click'); }, 2000);
	
	//bind the customer period event
	$('#customer-period-list li a').click(function(ev){
		ev.preventDefault();
		var _this = $(this);
		$('#customer-period-text').html(_this.html());
		$('#customer-period-date').html(_this.data('value'));
		$('#customer-period-selected').val(_this.data('type'));
		$('#customer-report-btn').trigger('click');
	});
});

function load_offers_stats() {
	$.ajax({
		'type':'GET', 'url':'<?php echo Yii::app()->createUrl('/offer/stats'); ?>', 'data':{},
		beforeSend:function(){$('#offers-stats-error').hide();},
		success:function(response) {
			$('#offers-stats').html(response);
		},
		error:function() {
			$('#offers-stats-error').show();
		}
	});
}

function load_recent_offers() {
	$.ajax({
		'type':'GET', 'url':'<?php echo Yii::app()->createUrl('/offer/recent'); ?>', 'data':{},
		beforeSend:function(){$('#offers-recent-error').hide();},
		success:function(response) {
			$('#offers-recent').html(response);
		},
		error:function() {
			$('#offers-recent-error').show();
		}
	});
}

function load_recent_customers() {
	$.ajax({
		'type':'GET', 'url':'<?php echo Yii::app()->createUrl('customer/list') ?>', 'data':{'sort':'recent', 'limit':5},
		beforeSend:function(){$('#customers-recent-error').hide();},
		success:function(response) {
			$('#customers-recent').html(response);
		},
		error:function() {
			$('#customers-recent-error').show();
		}
	});
}

function load_customer_report(type) {
	$.ajax({
		'type':'get', 'url':'<?php echo Yii::app()->createUrl('/customer/report'); ?>', 'data':{type:type},
		beforeSend:function(){$('#customer-report-error').hide();},
		success:function(response) {
			$('#customer-report').html(response);
		},
		error:function() {
			$('#customer-report-error').show();
		}
	});
}
</script>